﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;

namespace OpenLS.Spreadsheet.Formats.Biff.OfficeArt
{
    internal class OfficeArtSolverContainer: OfficeArtRecord
    {
        private List<OfficeArtSolverContainerFileBlock> rgfb = new List<OfficeArtSolverContainerFileBlock>();

        public override RecType RecordType
        {
            get { return RecType.OfficeArtSolverContainer; }
        }

        

        protected override void ReadNoHeader(OfficeArtReadContext context)
        {
            Contract.Assert(rhrecVer == 0xF);
            Contract.Assert(rhrecType == (RecType) 0xF005);
            Contract.Assert(rhrecVer == 0xF);
            for (int i = 0; i < rhrecInstance; i++)
            {
                var item = OfficeArtSolverContainerFileBlock.Read(context.Reader);
                //item.Read(context.Reader);
                rgfb.Add(item);
            }
        }

        protected override byte Version
        {
            get { return 0xF; }
        }

        protected override int ByteLength
        {
            get { return Enumerable.Sum(rgfb, item => item.ByteLength); }
        }


        protected override void WriteNoHeader(OfficeArtWriteContext context)
        {
            Contract.Assert(rhrecVer == 0xF);
            Contract.Assert(rhrecType == (RecType)0xF005);
            Contract.Assert(rhrecVer == 0xF);
            foreach (var item in this.rgfb)

                item.Write(context.Writer);
        }

        protected override void PrepareHeader(ref OfficeArtRecordHeader rh)
        {
            rh.recVer = 0xF;
            rh.recInstance = (ushort) this.rgfb.Count;
            rh.recLen = (uint) this.ByteLength;

        }
    }
}